<%--
  Created by IntelliJ IDEA.
  User: Tim-Lee
  Date: 2018/3/29
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>hello</title>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('a').click(function () {
                var fileName = $(this).attr("fileName");
                $.ajax({
                    url: '${pageContext.request.contextPath}/checkFileExists',
                    type: 'post',
                    data: {fileName: fileName},
                    dataType: 'json',
                    success: function (data) {
                        if (data.success) {
                            //文件存在开始下载
                            /*                           $("#myForm").attr("action","/"+data.url)
                                                       $("#myForm").submit();*/
                            location.href = '${pageContext.request.contextPath}/' + data.url;
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function () {

                    }
                });
            });
        });

    </script>
</head>
<body>


<p>
    <a href="javascript:;" download="我的文件" fileName="美女.jpg">下载文件</a>
</p>

<form action="" method="POST" id="myForm"></form>
</body>
</html>
